pipeline {

    agent {
        kubernetes {
            defaultContainer 'maven'
            yamlFile '../podTemplates/maven-3.8.4-openjdk-11.yaml'
        }
    }

    stages {

        stage('Code Clone') {
            echo "1. Clone Stage"
            steps {
                git branch: 'docker', credentialsId: '3a5b6f3c-402f-4bab-a107-3c4e690c607f', url: 'http://192.168.110.35/root/GeneLab.git'
                container('git'){
                    script {
                        build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                    }
                }
                echo "${build_tag}"
            }
        }

        stage('Run Maven') {
            echo "2. Build Jar Stage"
            steps {
                sh 'mvn install -N && mvn clean package -pl knowledge/KnowledgeCloud/ -am -Dmaven.test.skip=true'
            }

        }

        stage('Build') {
            echo "3.Build Docker Image Stage"
            sh "docker build -t 192.168.110.72/gene/knowledgecloud:${build_tag} ./knowledge/KnowledgeCloud"
        }

        stage('Push') {
            echo "4.Push Docker Image Stage"
            withCredentials([usernamePassword(credentialsId: '63175724-81b9-4c17-a1d3-e7d3b0e7dcfb', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
                sh "docker login harbor -u ${dockerHubUser} -p ${dockerHubPassword}"
                sh "docker push 192.168.110.72/gene/knowledgecloud:${build_tag}"
            }
        }

    }
}